<template>
    <div class="app-container calendar-list-container" v-loading="loading" element-loading-text="正在导出数据">
    <!-- filter 条件查询 -->
    <div class="filter-container">
      <el-input
            @keyup.enter.native="getData"
            clearable
            v-model="listQuery.name"
            style="width: 150px;margin-right:15px;"
            class="filter-item"
            placeholder="请输入员工姓名"
      ></el-input>

      <el-input
            @keyup.enter.native="getData"
            clearable
            v-model="listQuery.jobNumber"
            style="width: 150px;margin-right:15px;"
            class="filter-item"
            placeholder="请输入员工工号"
      ></el-input>

      <el-date-picker
        v-model="listQuery.startDate"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="选择开始生产日期"
        style="width: 180px;margin-right:15px;" class="filter-item"
      >
      </el-date-picker>
      <el-date-picker
        v-model="listQuery.finishDate"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        placeholder="选择结束生产日期"
        style="width: 180px;margin-right:15px;" class="filter-item"
      ></el-date-picker>

      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="getData">搜索</el-button>

    </div>
    
    <!-- table 表格 -->
    <el-table  :data="tableInfo" v-loading="loadingList" element-loading-text="给我一首歌的时间" stripe border fit highlight-current-row style="width: 100%">
      <el-table-column  label="序号" type="index" align="center" width="80px"/>
      <el-table-column label="员工工号"  prop="jobNumber" align="center"/>
      <el-table-column label="员工姓名"  prop="userName" align="center"/>
      <el-table-column  label="原质量系数" prop='initial' align="center"/>
      <el-table-column  label="扣罚系数" prop='punishCoefficient' align="center"/>
      <el-table-column  label="扣罚原因" prop='afineCause' align="center"/>
      <el-table-column  label="最终质量系数" prop='ultimately' align="center"/>
    </el-table>
   
   <!-- pagination 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.page"
        :page-sizes="[5,10, 20, 30, 40, 100, 300, 500]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

     <el-divider></el-divider>

    <el-button type="success" size="small" @click="hExportExcel">导出数据Excel</el-button>

  </div>
</template>

<script>
import { getUseIntradayQuality } from '@/api/performance.js'
export default {
    name: 'massCoefficient',
    data() {
        return {
            total: 0,                           //数据的总数
            listQuery: {
                startDate: '',                    //开始生产日期
                finishDate: '',                      //结束生产日期
                jobNumber: '',                    //员工工号
                name: '',                         //员工姓名
                page: 1,                          //页码
                limit: 10                        //每页显示的数据条数
            },
            tableInfo: [],
            loadingList: false,
            loading: false
        }
    },
    methods: {
    transformDat(res) {
      const mapInfo = {
        userName: '员工姓名',
        jobNumber: '员工工号',
        initial: '原质量系数',
        punishCoefficient: '扣罚系数',
        afineCause: '扣罚原因',
        ultimately: '最终质量系数'
      }
      // 对data进行格式化
      // 取出所有的属性名
      const props = Object.keys(res[0]) // ['id','']
      console.log(props)
      // 将属性名从英文转为中文
      const header = props.map(zhkey => mapInfo[zhkey])
      console.log('中文的表头', header)
      // const data=[['xxxx','总裁办'，'13800000']]
      const data = res.map(obj => Object.values(obj))
      console.log(data)
      return { header, data }
    },
    // 导出excel
    async hExportExcel() {
      this.loading = true
      let query = Object.assign({},this.listQuery)
      query.date = query.date == null ? '' : query.date
      query.page = 1
      query.limit = 10000
      console.log('%c导出excel传的参数query:','color:green;font-size:16px;')
      console.log(query)

      let arr = await getUseIntradayQuality(query)
      let nameString = query.name?query.name + '-':''
      let jobNumberString = query.jobNumber?'工号' + query.jobNumber + '-':''
      let dateString = query.date?'生产日期' + query.date + '-':''
      let nameLengthen = nameString + jobNumberString + dateString
      const { header, data } = this.transformDat(arr.data.UseIntradayQuality)
        import('@/vendor/Export2Excel').then(excel => {
        // excel表示导入的模块对象
        console.log(excel)
        excel.export_json_to_excel({
        header, // 表头 必填
        data, // 具体数据 必填
        filename: nameLengthen + '质量系数excel', // 文件名称
        autoWidth: true, // 宽度是否自适应
        bookType: 'xlsx' // 生成的文件类型
        })
      })
      this.loading = false
    },
    // 搜索按钮绑定的函数，向服务器发送表单数据
    // 所有传输的表单字段都不是必须的，按需查询
    async handleFilter() {
      let query = Object.assign({},this.listQuery)
      query.startDate = query.startDate == null ? '' : query.startDate
      query.finishDate = query.finishDate == null ? '' : query.finishDate
      // 开始生产日期以及结束生产日期必须同时选择或者同时不选择
      if(query.startDate) {
        if(!query.finishDate) return this.$message({
                message: '请选择结束生产日期',
                type: 'warning'
            })
      } else {
        if (query.finishDate) return this.$message({
                message: '请选择开始生产日期',
                type: 'warning'
            })
      }
      this.loadingList = true
      console.log('%c/device/getUseIntradayQuality接口传参:','color:red;font-size:16px;')
      console.log('query',query)
      let {data} = await getUseIntradayQuality(query)
      this.tableInfo = data.UseIntradayQuality
      this.total = data.total
      this.loadingList = false
    },
    // table表单下方的页码组件改变每页显示的数据条数
     handleSizeChange(val) {
      this.listQuery.limit = val
      this.handleFilter()
    },
    // table表单下方的页码组件改变页面
     handleCurrentChange(val) {
      this.listQuery.page = val
      this.handleFilter()
    },
    getData() {
      this.listQuery.page = 1
      this.listQuery.limit = 10
      this.handleFilter()
    },
  },
  // 进入页面获取table信息
  mounted() {
    this.handleFilter()
  }
}
</script>

<style>

</style>